package basiclocaloperator.parsexml;

import org.xml.sax.*;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

public class SAXParse {
    public static void main(String[] args) throws Exception {
        //1创建解析工程
        SAXParserFactory factory = SAXParserFactory.newInstance();
        //2得到解析器
        SAXParser saxParser = factory.newSAXParser();
        //3得到读取器
        XMLReader reader = saxParser.getXMLReader();
        //4设置内容处理
        reader.setContentHandler(new MyHandler());
        //5读取xml内容
        reader.parse("./web/xmldtd/elementeg/book.xml");
    }
}

class MyHandler implements ContentHandler {

    @Override
    public void setDocumentLocator(Locator locator) {

    }

    @Override
    public void startDocument() throws SAXException {

    }

    @Override
    public void endDocument() throws SAXException {

    }

    @Override
    public void startPrefixMapping(String prefix, String uri) throws SAXException {

    }

    @Override
    public void endPrefixMapping(String prefix) throws SAXException {

    }

    @Override
    public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException {
        System.out.println(qName);
    }

    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
        System.out.println(qName);
    }

    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
        System.out.println(new String(ch, start, length));
    }

    @Override
    public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException {

    }

    @Override
    public void processingInstruction(String target, String data) throws SAXException {

    }

    @Override
    public void skippedEntity(String name) throws SAXException {

    }
}
